package com.kyzh.core.pager.weal.share

import android.os.Bundle
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.google.android.material.tabs.TabLayout
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.adapters.ShareHistoryTabAdapter
import kotlinx.android.synthetic.main.activity_share_history.*

class ShareHistoryActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_share_history)

        val adapter = ShareHistoryTabAdapter(this, arrayListOf(getString(R.string.inviteFriend), getString(R.string.earnRewards)))
        viewPager.adapter = adapter
        viewPager.currentItem = 0
        tabLayout.setupWithViewPager(viewPager)
        for (i in 0 until tabLayout.tabCount) {
            val tab: TabLayout.Tab? = tabLayout.getTabAt(i)
            tab?.customView = adapter.getTabView(i)
        }
        val textView1 = tabLayout.getTabAt(0)?.customView?.findViewById<TextView>(R.id.root)
        val textView2 = tabLayout.getTabAt(1)?.customView?.findViewById<TextView>(R.id.root)
        textView1?.setBackgroundResource(R.drawable.couponleft_select)
        textView2?.setBackgroundResource(R.drawable.couponright_unselect)
        textView1?.setTextColor(ContextCompat.getColor(this@ShareHistoryActivity,R.color.white))
        textView2?.setTextColor(ContextCompat.getColor(this@ShareHistoryActivity,R.color.font_33))
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab) {

                val textView = tab.customView?.findViewById<TextView>(R.id.root)
                if (tab.position==0){
                    textView?.setBackgroundResource(R.drawable.couponleft_select)
                    textView?.setTextColor(ContextCompat.getColor(this@ShareHistoryActivity,R.color.white))
                }else{
                    textView?.setBackgroundResource(R.drawable.couponright_select)
                    textView?.setTextColor(ContextCompat.getColor(this@ShareHistoryActivity,R.color.font_33))
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {

                val textView = tab.customView?.findViewById<TextView>(R.id.root)
                if (tab.position==0){
                    textView?.setBackgroundResource(R.drawable.couponleft_unselect)
                    textView?.setTextColor(ContextCompat.getColor(this@ShareHistoryActivity,R.color.font_33))
                }else{
                    textView?.setBackgroundResource(R.drawable.couponright_unselect)
                    textView?.setTextColor(ContextCompat.getColor(this@ShareHistoryActivity,R.color.font_33))
                }
            }

            override fun onTabSelected(tab: TabLayout.Tab) {

                val textView = tab.customView?.findViewById<TextView>(R.id.root)
                if (tab.position==0){
                    textView?.setBackgroundResource(R.drawable.couponleft_select)
                    textView?.setTextColor(ContextCompat.getColor(this@ShareHistoryActivity,R.color.white))
                }else{
                    textView?.setBackgroundResource(R.drawable.couponright_select)
                    textView?.setTextColor(ContextCompat.getColor(this@ShareHistoryActivity,R.color.white))
                }
            }
        })
    }
}